package com.hazardSoft.eStore.bussines;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.hazardSoft.eStore.common.exception.SystemException;
import com.hazardSoft.eStore.entities.Brand;
import com.hazardSoft.eStore.entities.Category;
import com.hazardSoft.eStore.entities.City;
import com.hazardSoft.eStore.entities.Department;
import com.hazardSoft.eStore.entities.OfferType;
import com.hazardSoft.eStore.entities.Role;
import com.hazardSoft.eStore.utils.PathConstants;
import com.hazardSoft.eStore.utils.QueryConstants;

public class ListBO {

	private static final String CLASS_NAME = PathConstants.BUSSINES_PATH+ListBO.class.getSimpleName();

	public List<Category> listCategories( EntityManager entityManager) throws SystemException{

		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_CATEGORIES);

		return query.getResultList();
	}

	public List<Department> listDepartments( EntityManager entityManager) throws SystemException{

		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_DEPARTMENTS);

		return query.getResultList();
	}

	public List<City> listCities( EntityManager entityManager) throws SystemException{

		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_CITIES);

		return query.getResultList();
	}

	public List<City> listCitiesByDepartment( Department department , EntityManager entityManager) throws SystemException{

		Department foundDepartemt = this.searchDepartmentById(department, entityManager);

		if (!(foundDepartemt == null)) {
			Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_CITIES_BY_DEPARTMENT);
			query.setParameter(1, foundDepartemt);
			
			return query.getResultList();
		}
		return null;
	}
	
	public Department searchDepartmentById(Department department, EntityManager entityManager) throws SystemException {
		if (department == null) {
			throw new SystemException(new Exception("null Department"), CLASS_NAME, "searchDepartmentById");
		}
		if (department.getId() == 0) {
			throw new SystemException(new Exception("null Department.Id"), CLASS_NAME, "searchDepartmentById");
		}
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_SEARCH_DEPARTMENT_BY_ID);
		query.setParameter(1, department.getId());
		
		return (Department) query.getSingleResult();
	}
	
	public List<OfferType> listOfferTypes( EntityManager entityManager) throws SystemException	{
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_OFFER_TYPES);

		return query.getResultList();
	}
	
	public List<Role> listRoles( EntityManager entityManager) throws SystemException {
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_ROLES);

		return query.getResultList();
	}
	
	public List<Brand> listBrands( EntityManager entityManager) throws SystemException {
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_BRANDS);

		return query.getResultList();
	}
}
